package io.renren.modules.sq.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.renren.common.utils.ResponseData;
import io.renren.modules.sq.entity.SqRoleSystemEntity;
import io.renren.modules.sq.entity.SqSystemEntity;
import io.renren.modules.sq.service.SqRoleSystemService;
import io.renren.modules.sq.service.SqSystemService;
import io.renren.modules.sys.entity.SysRoleEntity;
import io.renren.modules.sys.entity.SysUserEntity;
import io.renren.modules.sys.entity.SysUserRoleEntity;
import io.renren.modules.sys.service.SysRoleService;
import io.renren.modules.sys.service.SysUserRoleService;
import io.renren.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/**
 * 角色系统表
 *
 * @author txw_cyl
 * @email sunlightcs@gmail.com
 * @date 2022-05-14 19:29:39
 */
@Api(tags = "角色管理")
@RestController
@RequestMapping("sys/sqrolesystem")
public class SqRoleSystemController {
    @Autowired
    private SqRoleSystemService sqRoleSystemService;

    @Autowired
    private SqSystemService sqSystemService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    @Autowired
    private SysRoleService sysRoleService;

    @ApiOperation("获取当前角色下系统应用列表")
    @PostMapping("/listbyuserid")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id",paramType = "query",dataType = "String",required = true),
            @ApiImplicitParam(name = "roleId",value = "角色id",paramType = "query",dataType = "Long",required = true)
    })
    public ResponseData<List<SqSystemEntity>> listbyuserid(@RequestParam("userId") String userId,
                                                            @RequestParam("roleId") Long roleId){
        //获取用户
        SysUserEntity user = sysUserService.getById(userId);
        if (user == null)   return ResponseData.success("用户不存在！", false);

        List<SqSystemEntity> res = null;
        if(user.getUserId() == user.getCreateUserId()){
            //最高级用户，返回所有系统
            res = sqSystemService.listbyroleid(roleId.intValue());
        }else {
            //其余用户
            res = sqSystemService.listbyuserid(userId, roleId.intValue());
        }
        return ResponseData.success(res);
    }

    @ApiOperation("为角色赋予系统权限")
    @PostMapping("/addRoleSystemrule")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids", value = "系统ids", paramType = "query", dataType = "array[Long]", allowMultiple = true, required = true),
            @ApiImplicitParam(name = "userId",value = "用户id",paramType = "query",dataType = "Long",required = true),
            @ApiImplicitParam(name = "roleId",value = "角色id",paramType = "query",dataType = "Long",required = true)
    })
    public ResponseData addRoleSystemrule(@RequestParam("ids") List<Long> ids,
                                        @RequestParam("userId") Long userId,
                                        @RequestParam("roleId") Long roleId){
        //查看用户是否存在此角色
        //获取用户
        SysUserEntity user = sysUserService.getById(userId);
        if (user == null)   return ResponseData.success("用户不存在！", false);

        if(user.getUserId() != user.getCreateUserId()) {
            //SysUserRoleEntity one = sysUserRoleService.getOne(new QueryWrapper<SysUserRoleEntity>().eq("user_id", userId).eq("role_id", roleId).last("limit 1"));
            SysRoleEntity one = sysRoleService.getOne(new QueryWrapper<SysRoleEntity>().eq("create_user_id", userId).eq("role_id", roleId).last("limit 1"));
            if (one == null) return ResponseData.success("该用户不存在该角色", false);
        }
        boolean flag = sqRoleSystemService.addRoleSystemrule(ids,roleId);
        return ResponseData.success("添加成功");
    }

}
